﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity;
using BAL;
using Common;
using System.Collections.Generic;

namespace Web
{
    public partial class PostSchool : System.Web.UI.Page
    {
        private int editid = 0;
        private int delid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "MasterSchool"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }

            if (Request.QueryString["editid"] != null)
            {
                editid = Convert.ToInt32(Request.QueryString["editid"].Trim());
            }
            if (Request.QueryString["delid"] != null)
            {
                delid = Convert.ToInt32(Request.QueryString["delid"].Trim());
            }
            if (!IsPostBack)
            {
                if (editid > 0)
                {
                    SchoolInfo schoolInfo = new SchoolInfo();
                    schoolInfo = School.GetSingleInfo(editid);
                    SchoolKeyword.Text = schoolInfo.Keyword.ToString();
                    SchoolName.Text = schoolInfo.Name.ToString();
                    BindArea(schoolInfo.AreaId.ToString());
                }
                else
                {
                    BindArea("");
                }
                if (delid > 0)
                {
                    if (!UserValidate.CheckLogin(this.Request, "DelSchool"))
                    {
                        Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                        Response.Redirect("login.aspx");
                        Response.End();
                    }
                    School.DelInfo(delid);
                    Utility.ReloadTopPage(this);
                }
                
            }
        }
        protected void BindArea(string selected)
        {
            IList<AreaInfo> list = Area.GetList();
            this.drpArea.DataTextField = "Name";
            this.drpArea.DataValueField = "Id";
            this.drpArea.DataSource = list;
            this.drpArea.DataBind();
            if (!PageValidate.IsEmpty(selected))
            {
                this.drpArea.SelectedValue = selected;
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (SchoolName.Text.Trim() == "")
            {
                Utility.ShowJsAlert("学校名称不能为空", this);
                return;
            }
            SchoolInfo schoolInfo = new SchoolInfo();
            schoolInfo.Keyword = SchoolKeyword.Text.Trim();
            schoolInfo.Name = SchoolName.Text.Trim();
            schoolInfo.AreaId = int.Parse(this.drpArea.SelectedItem.Value);
            schoolInfo.AreaName = this.drpArea.SelectedItem.Text;
            if (editid > 0)
            {
                schoolInfo.Id = editid;
            }
            School.Save(schoolInfo);
            Utility.ReloadTopPage(this);
        }
    }
}
